<?php

namespace app\home\lib;

use think\Controller;
use think\Request;
use think\Session;
use think\Cache;

class MyController extends Controller
{
	protected $cacheTime = 30; //缓存时间（秒）
	protected $onePage = 20;

	public function _initialize()
	{
		parent::_initialize();
		// echo "服务器维护中，请稍后····<br/><br/>";
		$this->checkLogin();
	}

	private function checkLogin()
	{
		$domain = request()->domain();
		// https://jiankangpao.sichuancancer.org/azfzjkp/home/login/login.html
		// var_dump($domain);
		// die();
		// if ($domain !== 'http://192.168.8.130') {
		// 	echo "登录链接不对哦<a href='http://192.168.8.130/azfzjkp/home/login/login.html'>____</a><br/><br/>";
		// 	// 为了安全后台只能IP通过VPN登录
		// 	// $url = "http://192.168.8.130/azfzjkp/home/login/login.html";
		// 	// header("Location: {$url}");
		// 	die();
		// }

		$url = request()->url();
		if (strpos($url, "test") == true) {
			return;
		}
		if (strpos($url, "login") == false && strpos($url, "phpinfo") == false) {
			$admin_login_status = Session("admin_login_status");
			if ($admin_login_status != 'OK') {
				$this->redirect('Login/login', array(), 1, '请先登录...');
			} else if (session('NOW_ENV') != NOW_ENV) {
				// 切换环境session失效
				session(null);
				$this->redirect('Login/login', array(), 1, '请先登录...');
			}
		}

		// if (strpos($_SERVER['DOCUMENT_URI'], '/btcws_dev/') !== false) {
			// 开发环境-用于刷新css
			$_SESSION['time'] = time();
		// } else {
		// 	// 生产环境 (用登录时间，每次重新登录都重新取)
		// 	$_SESSION['time'] = $user_login_time;
		// }

		$items = [];
		$items[] = [
			'icon' => 'fa-money',
			'active' => '',
			'name' => '系统管理',
			'list' => [
				// [
				// 	'active' => '',
				// 	'name' => '问答管理',
				// 	'url' => url('home/Qa/index'),
				// 	'param' => []
				// ],
				[
					'active' => '',
					'name' => '问答记录',
					'url' => url('home/Qa/recordList'),
					'param' => []
				],
				// $items[] = [
				// 	'icon' => 'fa-users',
				// 	'active' => '',
				// 	'name' => '用户管理',
				// 	'list' => [
				// [
				// 	'active' => '',
				// 	'name' => '小程序用户',
				// 	'url' => url('home/User/index'),
				// 	'param' => []
				// ],
				// [
				// 	'active' => '',
				// 	'name' => '操作记录',
				// 	'url' => url('home/MyLog/list'),
				// 	'param' => []
				// ]
			]
		];

		$rPath = request()->path();
		$find = false;
		foreach ($items as $k1 => $v1) {
			foreach ($v1['list'] as $k2 => $v2) {
				if (strpos($v2['url'], $rPath) !== false) {
					$items[$k1]['active'] = 'active';
					$items[$k1]['list'][$k2]['active'] = 'active';
					$find = true;
					break;
				}
			}
			if ($find) {
				session('leftMenuItems', $items);
				break;
			}
		}
		$leftMenuItems = session('leftMenuItems');
		// var_dump($rPath);
		// var_dump($items);

		// $this->assign('leftMenuItems', $items);
		$this->assign('leftMenuItems', $leftMenuItems ? $leftMenuItems : $items);
	}

	function __destruct()
	{
		$request = Request::instance();
		// 更新操作后清空数据库缓存
		if (in_array($request->action(), ['edit', 'delete', 'put_banner', 'lot_edit', 'lot_delete', 'do_order', 'consultant_delete'])) {
			Cache::clear("db_cache");
		}
	}
}
